//
//  NXTRemoteControllerAppDelegate.m
//  NXTRemoteController
//
//  Created by naceka on 31.01.12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "NXTRemoteControllerAppDelegate.h"

@implementation NXTRemoteControllerAppDelegate

@synthesize window;

@synthesize modeChanger;
@synthesize settingsBtn;
@synthesize btStateBtn;
@synthesize deviceAddr;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
    
    [self.window makeKeyAndVisible];
	
	//[lblBtState setText:@"OFF"];
	//[lblDeviceState setText:@"OFF"];
	
	mode = CONTROL;	//mode of program
	
	//robot
	robot = [[Robot alloc] init];
	
	//add Panel
	panel = [[Panel alloc] initWithFrame:CGRectMake(0, 106, 768, 144) andRobot:robot];
	[window addSubview:panel];
	
	//set delegate for sensor data
	[robot setDelegate:panel];
	
	//add Control
	control = [[Control alloc] initWithFrame:CGRectMake(0, 0, 768, 684) andRobot:robot];
	//[window addSubview:control];
	
	//add Track
	track = [[Track alloc] initWithFrame:CGRectMake(0, 0, 768, 684) andRobot:robot];
	//[window addSubview:track];
	
	
	//container for animation
	UIView * containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 340, 768, 684)];
	[containerView addSubview:control];
	[window addSubview:containerView];
	
	//segmented control -> slot
	[modeChanger addTarget:self action:@selector(switchViews:) forControlEvents:UIControlEventValueChanged];
	
	//settings
	settings = [[SettingsUIViewController alloc] initWithNibName:nil bundle:nil andRobot:robot];
	[settingsBtn addTarget:self action:@selector(settingsPressed:) forControlEvents:UIControlEventTouchUpInside];
			
	//set button for red image
	[self deviceDisconnected];
	
	//register in notification center
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceConnected) name:@"RobotDeviceConnected" object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceDisconnected) name:@"RobotDeviceDisconnected" object:nil];
	
	return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive.
     */
}


- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     */
}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}


- (void)dealloc {
	NSLog(@"dealloc");
    [window release];
    [super dealloc];
}

//animation of view changing
- (IBAction) switchViews:(id) sender {
	NSLog(@"WE are here!");
	
	UIViewAnimationOptions options;
	
	UIView * fromView;
	UIView * toView;
	
	if (mode == CONTROL) {
		fromView = control;
		toView = track;
		options = UIViewAnimationOptionTransitionFlipFromLeft;
		
		mode = TRACK;
	}
	else {
		fromView = track;
		toView = control;
		options = UIViewAnimationOptionTransitionFlipFromRight;
		
		mode = CONTROL;
	}
	
	[UIView transitionFromView:fromView toView:toView duration:0.5 options:options|UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState completion:nil];
}

- (IBAction) settingsPressed:(id) sender {
	UINavigationController * content = [[UINavigationController alloc] initWithRootViewController:settings];
	popover = [[UIPopoverController alloc] initWithContentViewController:content];
	popover.popoverContentSize = CGSizeMake(450, 350);
	[popover presentPopoverFromRect:settingsBtn.frame inView:window permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}	

- (void) deviceConnected {
	deviceAddr.text = [robot getNxtId];
	[btStateBtn setBackgroundImage:[UIImage imageNamed:@"connected.png"] forState:UIControlStateDisabled]; 
	[popover dismissPopoverAnimated:YES];
	
	//set all widgets on
	[self setWidgetsEnabled:YES];
}

- (void) deviceDisconnected {
	[btStateBtn setBackgroundImage:[UIImage imageNamed:@"disconnected.png"] forState:UIControlStateDisabled];
	
	//set all widgets off
	[self setWidgetsEnabled:NO];
}

- (void) setWidgetsEnabled:(BOOL) state {
	[modeChanger setEnabled:state];
	control.userInteractionEnabled = state;
	track.userInteractionEnabled = state;
}

@end
